<?php
session_start();
?>
document.write('<link href="/css/login.css" rel="stylesheet" type="text/css" media="screen" />'); 
<?php
If(trim($_SESSION["USERNAME"])==""|trim($_SESSION["USERID"])=="")

{ 
?>
document.getElementById("vtip1").innerHTML = "<div style=\"width:200px;text-align:right;marmgin-bottom:6px;padding-bottom:6px;float:right;\"><input type='button' onclick='changeLogin(1);openlogin_div(1)' class=\"mylogin_01\"/> <input class=\"myreg_01\" type='button' class='les' onclick='changeLogin(2);openlogin_div(2)'/></div>";
<?php 
}else{
?>
document.getElementById("vtip1").innerHTML ='<?php echo($_SESSION["USERNAME"]); ?>欢迎您！<a href="member/index.php"  target="_blank">会员中心</a> <a href="#" onclick="exitLogon();">退出</a>';
<?php
}
?>	
var iners;
function changeLogin(loginType){
	//iners 表示登陆框 的 HTML 内容
	var formHtml = "";
	if(loginType == 1){

		formHtml = "<form name='LogonForm' method='post' onSubmit='return false' action='ckLogin.Asp'>"+

   		"<table width='100%' border='0' cellspacing='0'  align=\"left\" cellpadding='0'>" + 
   		"<tr><td><table width='10' border='0' cellspacing='0' cellpadding='0'></table></td><td>"+
   		"<table border='0' cellspacing='0' align=\"left\" cellpadding='0'>"+
		    "<tr align='left'>"+
		     " <td class='tl' width='200'><h6>会员登录</h6></td>"+
		    "</tr>"+
		   " <tr align='left'>"+
		      "<td width='200'><label>账号：<span>用户名</span></label></td>"+
		    "</tr>"+
		    "<tr>"+
		     " <td class='input' width='200'><input name='userName' type='text' id='userName'  class='sr' value='' tabindex=1/></td>"+
		   " </tr>"+
		   " <tr align='left'>"+
		      "<td><label>密码：</label></td>"+
		   " </tr>"+
		    "<tr>"+
		      "<td class='input' width='200'><input type='password' name='password' id='password' class='sr' tabindex=2/></td>"+
		    "</tr>"+
		    "<tr align='left'>"+
		      "<td><label>验证码：</label></td>"+
		    "</tr>"+
		    "<tr>"+
		      "<td class='input yzm' style=\"text-align:left\"><input type='text' name='checkcode' id='checkcode' class='sr' maxlength='5' tabindex=3/>"+
		        "<img src='data/include/imagecode.php?act=verifycode' id='yzImg' width='55' height='18'  onclick=\"this.src='data/include/imagecode.php?act=verifycode&t='+(new Date().getTime());\" alt=\"看不清楚\" /></td>"+
		    "</tr>"+
		    "<tr>"+
		      "<td rowspan='2' class='bt'><input name='sub' id='sub' type='button' class='bt_logdl' onclick='submitLogon();return false;' value='  登 录' tabindex=4/>"+
		        "<span class='f_666_12'><a href='member/getpassword.php' target='_blank'>忘记密码</a>？</span></td>"+
		    "</tr>"+
		    "</table>"+
		    "</td>"+
		    "</tr>"+
		  "</table>"+
		"</form>";
   
	}
	
	else{
	 formHtml =
	iners = "<form name='LogonForm' method='post' style=\"margin-top:0px;padding-top:0px\" onSubmit='return false' action='member/mysaveuser.php'>"+
   		"<input type='hidden' name='userCheck' id='userCheck' value='false'>" + 
  "<table width='100%' border='0' cellspacing='0' align=\"left\" cellpadding='0'>" + 
   		"<tr><td><table width='10' border='0' cellspacing='0' cellpadding='0'></table></td><td>"+
   		"<table border='0' cellspacing='0' align=\"left\" cellpadding='0'>"+
    "<tr>" + 
      "<td class=\"tr\" width='200' height=\"10px\"><font style=\"font-size:18px;font-weight:bold;color:#3866ab;\"></font></td>" +
    "</tr>" +
    "<tr align='left'>" + 
      "<td  width='200'><label>填写注册账号：<span>用户名</span></label></td>"+
    "</tr>" +
    "<tr>" + 
       "<td class='input' width='200'><input name='userName1' type='text' id='userName1'  class='sr' tabindex=5 /></td>"+
     "</tr>" +
    "<tr align='left'>" + 
      "<td><label>设置密码：</label></td>" +
    "</tr>" +
    "<tr>" + 
"<td class='input' width='200'><input type='password' name='password1' id='password1' class='sr' tabindex=6/></td>"+
	"</tr>"+
    "</tr>" +
    "<tr align='left'>" + 
      "<td><label>请再次输入密码：</label></td>" +
    "</tr>" +
    "<tr>" + 
"<td class='input' width='200'><input type='password' name='password2' id='password2' class='sr' tabindex=7/></td>"+
    "</tr>" +
    "<tr align='left'>" + 
      "<td><label>验证码：</label></td>" +
    "</tr>" +
    "<tr>" + 
"<td class='input yzm' style=\"text-align:left\"><input type='text' name='checkcode' id='checkcode' class='sr'  maxlength='5' tabindex=8/>"+
"<img src='data/include/imagecode.php?act=verifycode' id='yzImg' width='55' height='18'  onclick=\"this.src='data/include/imagecode.php?act=verifycode&t='+(new Date().getTime());\" alt=\"看不清楚\" /></td>"+
    "</tr>" +
    "<tr>" + 
 "<td class='bt'><input type='button' class='bt_logzc' onclick='submitRegiste();' value='  免费注册' tabindex=9/></td>"+
    "<tr>" + 
"</table>"+
		    "</td>"+
		    "</tr>"+
		  "</table>"+
  "</form>";
	}

		iners = "<div class='login-header-tools'><div class='loginPrompt_close' title='关闭' onclick='javascript:close_LoginDiv()'></div></div>"+
   	"<div class='box_win_log' id='box_content'>"+
   	formHtml +
	"</div>";
}


var docEle = function() 
{
    return document.getElementById(arguments[0]) || false;
}

function openlogin_div(loginType) 
{
    var m = "mask";
    if (docEle("login_div")) document.body.removeChild(docEle("login_div"));
    if (docEle(m)) document.body.removeChild(docEle(m));
    
    //mask遮罩层，作用是屏蔽底层页面控件
    var newMask = document.createElement("div");
    newMask.id = m;
    newMask.style.position = "absolute";
    newMask.style.zIndex = "1";
    _scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
    _scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
	_scrollHeight = Math.max(_scrollHeight,window.screen.availHeight);
	//alert("document.body.scrollHeight = " + document.body.scrollHeight +  "  document.documentElement.scrollHeight = " + document.documentElement.scrollHeight + "_scrollHeight = " +  _scrollHeight + "  window.screen.availHeight = " + window.screen.availHeight);
    newMask.style.width = _scrollWidth + "px";
    newMask.style.height = _scrollHeight + "px";
    newMask.style.top = "0px";
    newMask.style.left = "0px";
    newMask.style.background = "#33393C";
    newMask.style.filter = "alpha(opacity=42)";
    newMask.style.opacity = "0.40";
    document.body.appendChild(newMask);
    
    //遮罩层加上 iframe，防止下拉列表穿透层
    var ifra = document.createElement("iframe"); 
	ifra.src="javascript:false";
	ifra.style.cssText = "position:absolute;visibility:inherit; top:0px; left:0px; width:" + _scrollWidth+"px; height:"+_scrollHeight+"px;z-index:-1; filter:chroma(color=#ffffff)" ;
	newMask.appendChild(ifra);
    
    //新弹出层
    var login_div = document.createElement("div");
    login_div.id = "login_div";
    login_div.style.position = "absolute";
    login_div.style.zIndex = "9999";
    login_divWidth = 400;
    login_divHeight = 320;
    login_div.style.width = login_divWidth + "px";
    login_div.style.height = login_divHeight + "px";
	    
    login_div.style.top = (document.documentElement.scrollTop + document.documentElement.clientHeight/2 - login_divHeight/2) + "px";
    login_div.style.left = (document.body.scrollLeft + document.documentElement.clientWidth/2 - login_divWidth/2) + "px";
    login_div.style.background = "#ffffff";
    login_div.style.border = "1px solid #BEBDBD";
    login_div.style.padding = "5px";
    
    document.body.appendChild(login_div);
    
    //弹出层滚动居中
    function login_divCenter()
    {	        
        login_div.style.top = (document.documentElement.scrollTop + document.documentElement.clientHeight/2 - login_divHeight/2) + "px";
        login_div.style.left = (document.body.scrollLeft + document.documentElement.clientWidth/2 - login_divWidth/2) + "px";
    }
    if(document.all)
    {
        window.attachEvent("onscroll",login_divCenter);
    }
    else
    {
        window.addEventListener('scroll',login_divCenter,false);
    }
    
    //关闭新图层和mask遮罩层
    var newA = document.createElement("a");
    newA.href = "#";

    newA.onclick = function()
    {
        if(document.all)
        {
         window.detachEvent("onscroll",login_divCenter);
        }
        else
        {
         window.removeEventListener('scroll',login_divCenter,false);
        }
        document.body.removeChild(docEle("login_div"));
        document.body.removeChild(docEle(m));
        return false;
    }
    
    login_div.innerHTML = iners;
    login_div.appendChild(newA);
    
    if(loginType == 1){
    	document.getElementById("box_content").style.height = "280px";
    	document.getElementById("login_div").style.height = "320px";
    	}
    else{
    	document.getElementById("box_content").style.height = "310px";
    	document.getElementById("login_div").style.height = "340px";
    	}
}

function close_LoginDiv()
{
	var _id = "login_div";
	var m = "mask";
    if (docEle(_id)) document.body.removeChild(docEle(_id));
    if (docEle(m)) document.body.removeChild(docEle(m));
}

function chekKey1(keyCode){
	if(keyCode==13){document.getElementById('sub').click();}
}


function submitLogon()
{
	var sendUrl="";
	var username="";
//用户登陆
	document.getElementById("userName").value = document.getElementById("userName").value.toLowerCase();//将用户名转换成小写存储
  	if(document.getElementById("userName").value==""){
	  alert("请输入完整的用户名！");
	  document.getElementById("userName").focus();
	  return false;
	}
	if(document.getElementById("password").value==""){
	  alert("请输入密码！");
	  document.getElementById("password").focus();
	  return false;
	}
  	if(document.getElementById("checkcode").value==""){
	  alert("请输入验证码！");
	  document.getElementById("checkcode").focus();
	  return false;
	}
	var sendUrl= "UIAction=logon&userName=" + document.getElementById("userName").value + "&password=" + document.getElementById("password").value 
		+ "&checkcode=" + document.getElementById("checkcode").value + "&userType=1&logonType=1";

	$.ajax({
 	type	: "get",
	async	: true,
 	url 	: "member/userlogin.php",
 	data	: sendUrl,
 	success	: function(msg){

	


 	            if($.trim(msg)=="success"){
	            alert(document.getElementById("userName").value + ",欢迎您！");
				var username=document.getElementById("userName").value;
		    	close_LoginDiv();
document.getElementById("vtip1").innerHTML =username + '欢迎您！<a href="member/index.php" target="_blank">会员中心</a>-<a href="#" onclick="exitLogon();">退出</a>';
			    }else if($.trim(msg)=="code"){
			        alert("验证码码出问题？");
			    }
				else if($.trim(msg)=="error"){
			        alert("登录失败？");		    
			    }
		},
	error 	: function(){$("#span1").html("<img src='images/loading.gif' alt='' />");}
	}); 


}

function submitRegiste(){

	if(document.getElementById("userName1").value==""){
	  alert("请输入完整的用户名！");
	  document.getElementById("userName1").focus();
	  return false;
	}

	if(document.getElementById("password1").value==""){
	  alert("请输入密码！");
	  document.getElementById("password1").focus();
	  return false;
	}
	
	if(document.getElementById("password2").value==""){
	  alert("请再次输入密码！");
	  document.getElementById("password2").focus();
	  return false;
	}
	
	if(document.getElementById("password1").value != document.getElementById("password2").value){
		alert("输入的两次密码不一致，请检查！");
		return false;
	}
	
  	if(document.getElementById("checkcode").value==""){
	  alert("请输入验证码！");
	  document.getElementById("checkcode").focus();
	  return false;
	}
    var sendUrl = "uiaction=register&usename=" + document.getElementById("userName1").value + "&checkcode=" + document.getElementById("checkcode").value
    	+ "&password=" + document.getElementById("password1").value + "&isNewUser=true";
	$.ajax({
 	type	: "get",
	async	: true,
 	url 	: "member/mysaveuser.php",
 	data	: sendUrl,
 	success	: function(msg){
 	            if($.trim(msg)=="success"){
				var username= document.getElementById("userName1").value;
                close_LoginDiv();
				alert("恭喜您注册成功！");
document.getElementById("vtip1").innerHTML =username + '欢迎您！<a href="member/index.php" target="_blank">会员中心</a>-<a href="#" onclick="exitLogon();">退出</a>';
			    }else if($.trim(msg)=="usernameerror"){
			        alert("用户名或者密码及其验证码错误？");
			    }
				else if($.trim(msg)=="pwderror"){
			        alert("密码错误？");		    
			    }
				else if($.trim(msg)=="error"){
			        alert("注册失败？");		    
			    }
				else if($.trim(msg)=="youerror")
				{
			        alert("不好意思用户已存在,请另取一个名字？");
			    }
		},
	error 	: function(){$("#span1").html("<img src='images/loading.gif' alt='' />");}
	}); 
}



function doLogon(){
	try{
		addentryId();
	}catch(e){
	}
}

function doExit(){
	try{
		removeEntryId();
	}catch(e){
	}
}

function exitLogon(){//退出登陆

	var sendUrl="action=out";
	$.ajax({
 	type	: "post",
	async	: true,
 	url	    : "member/myoutlogin.php",
 	data	: sendUrl,
 	success	: function(msg){
 	        if(msg=="success"){
             alert("恭喜你安全退出");
			 session();
document.getElementById("vtip1").innerHTML = "<div style=\"width:200px;text-align:right;marmgin-bottom:6px;padding-bottom:6px;float:right;\"><input type='button' onclick='changeLogin(1);openlogin_div(1)' class=\"mylogin_01\"/> <input class=\"myreg_01\" type='button' class='les' onclick='changeLogin(2);openlogin_div(2)'/></div>";
		    	}
				else
				{
                  alert("退出错误？");
			    }
		},
	error 	: function(){$("#span1").html("<img src='images/loading.gif' alt='' />");}
	}); 

}



function session(){
	var sendUrl="action=out";
	$.ajax({
 	type	: "post",
	async	: false,
 	url	    : "member/myoutlogin.php",
 	data	: sendUrl,
 	success	: function(msg){
		},
	error 	: function(){$("#span1").html("请求失败");}
	}); 
}













function checkUserName(txt){

      if(document.getElementById("userName1").value==""){
  	    alert("请输入用户名！");
  	    document.getElementById("userCheck").value="false";
  	    return false;
      }     
      //alert(bytes(txt));
      if (bytes(txt) < 5 || bytes(txt) > 50){
	  alert("请正确输入用户名,长度应该为5-50个字符！");
		return false;
	}else if (!chkstr(txt) && !isEMail(txt)){
	  alert("用户名应该由小写字母、数字、下划线组成，或者为有效 email 地址！");
		return false;
	}
	return true;
}

//校验用户名必须是：5-50个字符(包括小写字母、数字、下划线、中文)，一个汉字为两个字符。
function bytes(str){
	if(typeof(str)!='string'){
		str = str.value;
	}
	var len = 0;
	for(var i = 0; i < str.length; i++){
		if(str.charCodeAt(i) > 127){
			len++;
		}
		len++;
	}
	return len;
}
function chkstr(str){
	if(typeof(str)!='string'){
		str = str.value;
	}
	for(var i = 0; i < str.length; i++){
		if (!str.substr(i,1).match(/^\w+$/ig)){//str.charCodeAt(i) < 127 && 
			return false;
		}
	}
	return true;
}
function checkItem(itm){
  if (bytes(itm) < 5 || bytes(itm) > 50){
	  alert("请正确输入用户名,长度应该为5-50个字符！");
		return false;
	}else if (!chkstr(itm)){
	  alert("请正确输入用户名,5-50个字符(包括小写字母、数字、下划线)！");//中文
		return false;
	}
	return true;
}


function isEMail(str){
	var regu = "^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|com|gov|mil|org|edu|int|NET|COM|GOV|MIL|ORG|EDU|INT)$";
    var re = new RegExp(regu);
	if (str.search(re) != -1) {
          return true;
    } else {
          return false;
    }
}


